﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Core.Events;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TABFOLGE : ViewBase
    {
     

        #region ==QPLAN===========
        public static readonly DependencyProperty QPLANProperty = DependencyProperty.Register("QPLAN",
            typeof(string), typeof(TABFOLGE),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPLANChanged));
        public string QPLAN
        {
            get
            {
                return (string)GetValue(QPLANProperty);
            }
            set
            {
                SetValue(QPLANProperty, value);
                ((VABFOLGE)MV).QPLAN = value;
            }
        }
        private static void OnQPLANChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABFOLGE;
            if (src != null)
            {
                src.QPLAN = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QPKEY===========
        public static readonly DependencyProperty QPKEYProperty = DependencyProperty.Register("QPKEY",
            typeof(string), typeof(TABFOLGE),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPKEYChanged));
        public string QPKEY
        {
            get
            {
                return (string)GetValue(QPKEYProperty);
            }
            set
            {
                SetValue(QPKEYProperty, value);

            }
        }
        private static void OnQPKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABFOLGE;
            if (src != null)
            {
                src.QPKEY = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QPKRS===========
        public static readonly DependencyProperty QPKRSProperty = DependencyProperty.Register("QPKRS",
            typeof(string), typeof(TABFOLGE),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPKRSChanged));
        public string QPKRS
        {
            get
            {
                return (string)GetValue(QPKRSProperty);
            }
            set
            {
                SetValue(QPKRSProperty, value);
                ((VABFOLGE)MV).QPKRS = value;
            }
        }
        private static void OnQPKRSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABFOLGE;
            if (src != null)
            {
                src.QPKRS = (string)e.NewValue;
            }
        }
        #endregion 
         
        #region ==dwABP===========
        public static readonly DependencyProperty dwABPProperty = DependencyProperty.Register("dwABP",
            typeof(DataRow), typeof(TABFOLGE),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwABPChanged));
        public DataRow dwABP
        {
            get
            {
                return (DataRow)GetValue(dwABPProperty);
            }
            set
            {
                SetValue(dwABPProperty, value);
            }
        }
        private static void OdwABPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABFOLGE;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwABP = row;
            }
        }
        #endregion

        #region ==AGANG===========
        public static readonly DependencyProperty AGANGProperty = DependencyProperty.Register("AGANG",
            typeof(string), typeof(TABFOLGE),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAGANGChanged));
        public string AGANG
        {
            get
            {
                return (string)GetValue(AGANGProperty);
            }
            set
            {
                SetValue(AGANGProperty, value);
                ((VABFOLGE)MV).AGANG = value;
            }
        }
        private static void OnAGANGChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABFOLGE;
            if (src != null)
            {
                src.AGANG = (string)e.NewValue;
            }
        }
        #endregion

        #region ==FTAUTOQC===========
        public static readonly DependencyProperty FTAUTOQCProperty = DependencyProperty.Register("FTAUTOQC",
            typeof(bool), typeof(TABFOLGE),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTAUTOQCChanged));
        public bool FTAUTOQC
        {
            get
            {
                return (bool)GetValue(FTAUTOQCProperty);
            }
            set
            {
                SetValue(FTAUTOQCProperty, value);
                ((VABFOLGE)MV).FTAUTOQC = value;
            }
        }
        private static void OnFTAUTOQCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABFOLGE;
            if (src != null)
            {
                src.FTAUTOQC = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==QCORDER===========
        public static readonly DependencyProperty QCORDERProperty = DependencyProperty.Register("QCORDER",
            typeof(string), typeof(TABFOLGE),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQCORDERChanged));
        public string QCORDER
        {
            get
            {
                return (string)GetValue(QCORDERProperty);
            }
            set
            {
                SetValue(QCORDERProperty, value);
                ((VABFOLGE)MV).QCORDER = value;
            }
        }
        private static void OnQCORDERChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABFOLGE;
            if (src != null)
            {
                src.QCORDER = (string)e.NewValue;
            }
        }
        #endregion
         
        
        public TABFOLGE()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABFOLGE();
            MESSAGES += "QCRELOAD;REFRESHBYGB33;LOADGP;CHKABF;";
            this.DataContext = MV;
            InitializeComponent();
            IsEdit = false;
            VABAUFM VABAUFM = new VABAUFM();
        }
        /// <summary> 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
         }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            
            if (dwMaster == null)
            {
                QCORDER = "000000000";
                QPKEY = "";
            }
            else
            {
                QCORDER = dwMaster["QOKORDER"].ToString();
                QPKEY = cQP02.QPKEY;
            }
        }
        
        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "QCRELOAD":
                    TBMain.Clear();
                    ((VABFOLGE)MV).QCRELOAD(msg);
                    vwMaster.View.MoveCurrentToFirst();
                    break;
                case "LOADGP":
                    LOADGP(msg);                     
                    break;
                case "REFRESHBYGB33": 
                    ((VABFOLGE)MV).REFRESHBYGB33(msg);
                    break;
                case "CHKABF":
                    TBMain.Clear();
                    CHKABF(msg);
                    break;
            }
        }
        public void CHKABF(MessageInfo msg)
        {
            TBMain.Clear();
            ((VABFOLGE)MV).LOADABF(msg);
             vwMaster.View.MoveCurrentToFirst();
        }
        
        public void LOADGP(MessageInfo msg)
        {
            TBMain.Clear();
            if (msg.PARTIE.Trim().Length == 0)
            {
                return;
            }
            ((VABFOLGE)MV).QCRELOAD(msg);
            /*if (TBMain.Rows.Count == 0)
            {
                QCNEW(msg);
            }
            else
            {
                vwMaster.View.MoveCurrentToFirst();
            }*/
            vwMaster.View.MoveCurrentToFirst();
        }       
       
        public override void SaveCommand(object sender, RoutedEventArgs e)
        { 
            if (dwMaster == null)
                return;
            VQOSTEP.Instance.UPDTEBYGB33(RUNID, dwMaster);
            base.SaveCommand(sender, e);
        }
         
        public void QCNEW(MessageInfo _msg)
        {
            MessageInfo msg = new MessageInfo(_msg);
            msg.Set = _msg.Set;
            msg.MSGTYPE = "QCNEW";
            msg.Sender = this.RUNID;
            msg.ACTION = "QCNEW";
            msg.AUFTYP = "V"; 
            if (QPLAN.Trim() == "")
                QPLAN = cQP02.QPKEY;
            if (QPLAN.Trim() == "")
                return;
            PM["RUNID"] = RUNID; 
            PM["ORDER"] = msg.ORDER;
            PM["P5"] = msg.P5; 
            PM["AUFTYP"] = msg.AUFTYP;
            PM["KRS"] = msg.KRS;
            PM["LFD"] = msg.LFD;
            PM["POS"] = msg.POS;
            PM["LIEFSCH"] = msg.LIEFSCH;
            PM["ART"] = msg.ART;
            PM["VAR"] = msg.VAR;
            PM["FARBE"] = msg.FARBE;
            PM["PARTIE"] = msg.PARTIE; 
            PM["DESS"] = msg.DESS;
            PM["COLORIT"] = msg.COLORIT;
            PM["BESCHICH"] = msg.BESCHICH;
            PM["GROE"] = msg.GROE;
             PM["QPLAN"] = QPLAN;
            PM["YCOUNT"] = msg.P1;
            PM["ROWQP"] = msg.Set.Tables[0].Rows[0];
            VQORDER.Instance.QCNEW(PM);
            VABAUFM.Instance.UpdateABAUFM(dwMaster, 9);
            //VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }     
       
    }
}
